<!DOCTYPE html>
<html>
  
<head>
  <meta charset="utf-8">
  <meta name="author" content="饿包子" />
  
  
  <title>Vue 项目的开始和扩展问题 | 饿包子博客</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="项目构建," />
  

  
  <meta name="description" content="叹世凡夫不悟空，迷花恋酒送英雄。春宵漏永欢娱促，岁月长时死限攻。弄巧常如猫捕鼠，光阴犹似箭离弓。不知使得精神尽，愿把身尸葬土中。吕洞宾 《指玄篇 上篇 其一》">

  
  
    <link rel="icon" href="/blog/images/xian.ico">
    <link rel="apple-touch-icon" href="/blog/images/Logo.png">
  

  
<link rel="stylesheet" href="/blog/css/Awesome.min.css">


  
<link rel="stylesheet" href="/blog/css/index.css">
<link rel="stylesheet" href="/blog/styles/components/highlight/highlight.css">


  
  
<script src="/blog/common/jquery.min.js"></script>

  
    
<script src="/blog/common/Av.min.js"></script>

  

  
    
<script src="/blog/common/MathJax.js"></script>

  

  
    
<script src="/blog/common/Valine.min.js"></script>

  

  

  <script>
  // theme-ad's config script
  // it can be used in every script
  
  window.AD_CONFIG = {
    leancloud: {"appid":"gE1EosDsVgB2slV4E92yPkcG-gzGzoHsz","appkey":"niR787UVTMyJQrR6gmOiJCKe","comment":true,"count":true},
    welcome: {"enable":false,"interval":30},
    start_time: "2018-06-10",
    passwords: ["a621ab606db2a11f63edc576a729843b8269250dc324206871d90635ac5e531c", ],
    is_post: true,
    lock: false,
    author: "饿包子",
    share: {"twitter":false,"facebook":false,"weibo":true,"qq":true,"wechat":false},
    mathjax: true,
    page_type: "",
    root: "/blog/"
  };
</script>

  
<script src="/blog/vendor/sha256.min.js"></script>
<script src="/blog/js/auth.js"></script>
<script src="/blog/js/index.js"></script>
<script src="/blog/vendor/qrcode.min.js"></script>


<meta name="generator" content="Hexo 5.4.2"></head>
  <body>
    <header class="site-header">
  <div class="site-header-brand">
    
      <span class="site-header-brand-title">
        <a href="/blog/">清风逐月</a>
      </span>
    
    
      <span class="site-header-brand-motto"> | 我思故我在</span>
    
  </div>
  <div class="site-header-right">
    <nav class="site-header-navigation">
      
        <a href="/blog/" target="_self">主页</a>
      
        <a href="/blog/archives/" target="_self">归档</a>
      
        <a href="/blog/tags/" target="_self">标签</a>
      
        <a href="/blog/categories/" target="_self">分类</a>
      
        <a href="/blog/categories/%E6%97%A5%E8%AE%B0" target="_self">日记</a>
      
    </nav>
    <div class="site-header-btn">
      
        <a href="https://github.com/lixianbin1/" target="_blank" id="site-github">
          <i class="fa fa-github-alt"></i>
        </a>
      
      <a href="javascript:void(0);" id="site-search">
        <i class="fa fa-search"></i>
      </a>
      <a href="javascript:void(0);" id="site-nav-btn">
        <i class="fa fa-ellipsis-v"></i>
      </a>
    </div>
  </div>
</header>
<nav class="table-content" id="site-nav">
  <div class="table-content-title">
    <span>导航</span>
  </div>
  <div class="table-content-main">
    <ol class="toc">
      
        <li class="toc-item">
          <a href="/blog/" target="_self">
            主页
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/archives/" target="_self">
            归档
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/tags/" target="_self">
            标签
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/" target="_self">
            分类
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/%E6%97%A5%E8%AE%B0" target="_self">
            日记
          </a>
        </li>
      
    </ol>
  </div>
</nav>
<div id="site-process"></div>
    <main>
      
  <div class="passage">
  <div class="passage-meta">
    <span>
      <i class="fa fa-calendar"></i>2020-12-09
    </span>
    
      <span>
        | <a href="/blog/categories/%E9%A1%B9%E7%9B%AE%E6%9E%84%E5%BB%BA/"><i class="fa fa-bookmark"></i>项目构建</a>
      </span>
    
    
      <span>
        | <i class="fa fa-unlock-alt"></i>UNLOCK
      </span>
    
    <span>
       | <i class="fa fa-calendar"></i>更新时间:2020-12-9 19:56 
    </span>
  </div>
  <h1 class="passage-title">
    Vue 项目的开始和扩展问题
  </h1>
  
    <div class="passage-cover">
      <figure style="background-image:url(/blog/images/posts/2020121002.jpg);"></figure>
    </div>
  
  <article class="passage-article">
    <p>Vue 是渐进式框架，它的数据是响应式的，基于数据是可变的，数据双向绑定，通过watch对属性进行监听，Vue通过数据劫持结合发布订阅者模式来实现数据双向绑定，它遍历data中的所有属性，通过Object.defineProperty 来劫持各个属性的 setter和getter，在数据变动时，以Dep算法遍历所有订阅者，发布消息触发回调，完成视图更新。</p>
<h2 id="使用须知"><a href="#使用须知" class="headerlink" title="使用须知"></a>使用须知</h2><p>文中举例，默认 yarn 和 @vue/cli 是已经安装了，如果没有安装，请先运行以下命令安装后；</p>
<blockquote>
<p>npm install yarn -g<br>  yarn add global add @vue/cli</p>
</blockquote>
<h2 id="开启项目"><a href="#开启项目" class="headerlink" title="开启项目"></a>开启项目</h2><blockquote>
<p>vue create xxx<br>  cd xxx<br>  yarn add vue-router<br>  yarn add vuex<br>  yarn add es6-promise   //可选，兼容低版本IE</p>
</blockquote>
<h2 id="示例代码"><a href="#示例代码" class="headerlink" title="示例代码"></a>示例代码</h2><figure class="highlight js"><figcaption><span>入口文件 main.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">&#x27;es6-promise/auto&#x27;</span>  <span class="comment">//引入低版本兼容polyfill</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Vue</span> <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span>      <span class="comment">//引入vue</span></span><br><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">&#x27;./Router&#x27;</span>  <span class="comment">//对路由的放在一个文件里</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;./vuex&#x27;</span>   <span class="comment">//将vuex放在一个文件里管理</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="property">config</span>.<span class="property">productionTip</span>=<span class="literal">false</span>  <span class="comment">//阻止vue启动生产消息</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">App</span>=(<span class="params"></span>)=&gt;<span class="language-xml"><span class="tag">&lt;<span class="name">router-view</span>&gt;</span><span class="tag">&lt;/<span class="name">router-view</span>&gt;</span></span> <span class="comment">//显示的是当前路由渲染的界面</span></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">render</span>: <span class="function"><span class="params">h</span>=&gt;</span><span class="title function_">h</span>(<span class="title class_">App</span>),  <span class="comment">//render方法渲染</span></span><br><span class="line">    router,</span><br><span class="line">    store</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app&#x27;</span>)</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><figcaption><span>路由集合 Router.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">Vue</span> <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">VueRouter</span> <span class="keyword">from</span> <span class="string">&#x27;vue-router&#x27;</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="title function_">use</span>(<span class="title class_">VueRouter</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 组件</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Home</span> <span class="keyword">from</span>  <span class="string">&#x27;./components/Home.vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Hello</span> <span class="keyword">from</span> <span class="string">&#x27;./components/Hello.vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Word</span> <span class="keyword">from</span> <span class="string">&#x27;./components/Word.vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Nofined</span> <span class="keyword">from</span> <span class="string">&#x27;./components/Nofined&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> routes=[</span><br><span class="line">  &#123;<span class="attr">path</span>:<span class="string">&#x27;/&#x27;</span>,<span class="attr">component</span>:<span class="title class_">Home</span>&#125;,</span><br><span class="line">  &#123;<span class="attr">path</span>:<span class="string">&#x27;/hello&#x27;</span>,<span class="attr">component</span>:<span class="title class_">Hello</span>, <span class="comment">//嵌套路由</span></span><br><span class="line">    <span class="attr">children</span>:[</span><br><span class="line">      &#123;<span class="attr">path</span>:<span class="string">&#x27;word&#x27;</span>,<span class="attr">component</span>:<span class="title class_">Word</span>&#125;,</span><br><span class="line">    ]</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;<span class="attr">path</span>:<span class="string">&quot;*&quot;</span>,<span class="attr">component</span>:<span class="title class_">Nofined</span>&#125; <span class="comment">//404页面</span></span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> router=<span class="keyword">new</span> <span class="title class_">VueRouter</span>(&#123;</span><br><span class="line">  routes</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> router</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><figcaption><span>状态管理 vuex.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">Vue</span> <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Vuex</span> <span class="keyword">from</span> <span class="string">&#x27;vuex&#x27;</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="title function_">use</span>(<span class="title class_">Vuex</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> moduleA = &#123; <span class="comment">// modulle管理</span></span><br><span class="line">  <span class="attr">state</span>:&#123;&#125;, <span class="attr">getters</span>:&#123;&#125;, <span class="attr">mutations</span>:&#123;&#125;, <span class="attr">actions</span>:&#123;&#125;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> <span class="title class_">Vuex</span>.<span class="title class_">Store</span>(&#123; <span class="comment">//以 Vuex.Store 方法创建实例 </span></span><br><span class="line">  <span class="attr">state</span>:&#123; <span class="comment">//状态</span></span><br><span class="line">    <span class="attr">name</span>:<span class="string">&quot;战争&quot;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">getters</span>:&#123; <span class="comment">//计算</span></span><br><span class="line">    <span class="attr">newName</span>:<span class="function"><span class="params">state</span>=&gt;</span>&#123;</span><br><span class="line">      state.<span class="property">name</span>==<span class="string">&quot;1&quot;</span>?<span class="keyword">return</span> <span class="string">&#x27;21&#x27;</span>:<span class="keyword">return</span> <span class="string">&#x27;11&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">mutations</span>:&#123;<span class="comment">//事件 commit</span></span><br><span class="line">    <span class="title function_">newName</span>(<span class="params">state,data</span>)&#123; <span class="comment">//设置名称</span></span><br><span class="line">      state.<span class="property">name</span>=data</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">actions</span>:&#123; <span class="comment">//行为 dispatch</span></span><br><span class="line">    <span class="attr">newName</span>:<span class="keyword">function</span>(<span class="params">context,data</span>)&#123;</span><br><span class="line">      context.<span class="title function_">commit</span>(<span class="string">&#x27;newName&#x27;</span>,data)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">modules</span>:&#123;</span><br><span class="line">    <span class="attr">one</span>:moduleA</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store</span><br></pre></td></tr></table></figure>

<h2 id="相关扩展"><a href="#相关扩展" class="headerlink" title="相关扩展"></a>相关扩展</h2><h3 id="在项目中配置模块的相关目录"><a href="#在项目中配置模块的相关目录" class="headerlink" title="在项目中配置模块的相关目录"></a>在项目中配置模块的相关目录</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">src/</span><br><span class="line"> |——api/ (所有请求：用户相关，功能相关等)</span><br><span class="line">   |——user.js </span><br><span class="line"> |——assets/ (静态资源：图片，视频，字体等)</span><br><span class="line">   |——images/ </span><br><span class="line"> |——components/ (所有组件：主页，登录，注册等)</span><br><span class="line">   |——Home.vue</span><br><span class="line"> |——config/ (所有配置项：网关,路由字体大小等)</span><br><span class="line">   |——geteway.js</span><br><span class="line">   |——router.js</span><br><span class="line"> |——store/  (Vuex的数据中心)</span><br><span class="line">   |——index.js</span><br><span class="line"> |——utils/  (工具集合：请求，常用函数等)</span><br><span class="line">   |——require.js</span><br><span class="line"> min.js</span><br></pre></td></tr></table></figure>
<h3 id="在手机端设置区分前进后退的代码"><a href="#在手机端设置区分前进后退的代码" class="headerlink" title="在手机端设置区分前进后退的代码"></a>在手机端设置区分前进后退的代码</h3><figure class="highlight js"><figcaption><span>路由集合 Router.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//区分前进后退，主要是为了在移动端添加不同的动画</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">push</span> = <span class="keyword">function</span>(<span class="params">url</span>)&#123;  <span class="comment">//路由前进</span></span><br><span class="line">    <span class="keyword">let</span> old=<span class="variable language_">this</span>.<span class="property">$router</span>.<span class="property">currentRoute</span>.<span class="property">path</span>  </span><br><span class="line">    <span class="keyword">if</span>(old==url)&#123; </span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">$router</span>.<span class="title function_">go</span>(<span class="number">0</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;  </span><br><span class="line">        <span class="title class_">Vue</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">transitionName</span> = <span class="string">&quot;slide-left&quot;</span>; <span class="comment">//通过设置不同的class触发动画  </span></span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">$router</span>.<span class="title function_">push</span>(url); </span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="title class_">Vue</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">pop</span> = <span class="keyword">function</span>(<span class="params">n=-<span class="number">1</span></span>)&#123;  <span class="comment">//路由后退</span></span><br><span class="line">    <span class="title class_">Vue</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">transitionName</span> = <span class="string">&quot;slide-right&quot;</span>;</span><br><span class="line">    <span class="keyword">let</span> path = <span class="variable language_">this</span>.<span class="property">$router</span>.<span class="property">currentRoute</span>.<span class="property">path</span>;</span><br><span class="line">    <span class="keyword">if</span> (path == <span class="string">&quot;/&quot;</span>) &#123; <span class="keyword">return</span>;&#125;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">$router</span>.<span class="title function_">go</span>(n);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="在本地项目中配置proxy解决跨域请求"><a href="#在本地项目中配置proxy解决跨域请求" class="headerlink" title="在本地项目中配置proxy解决跨域请求"></a>在本地项目中配置proxy解决跨域请求</h3><p>这是 <code>vue-cli</code>工具的配置，在项目的根目录创建 <code>vue.config.js</code> </p>
<figure class="highlight js"><figcaption><span>vue.config.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = &#123;</span><br><span class="line">  <span class="attr">devServer</span>: &#123; <span class="comment">//请求配置</span></span><br><span class="line">      <span class="attr">proxy</span>: &#123;</span><br><span class="line">        <span class="string">&quot;/user&quot;</span>: &#123; <span class="comment">//将路径匹配为 /user 的请求转发到该路径</span></span><br><span class="line">          <span class="attr">target</span>: <span class="string">&#x27;https://uat.happit.cn/&#x27;</span>,  <span class="comment">//转发路径</span></span><br><span class="line">          <span class="attr">changeOrigin</span>:<span class="literal">true</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="string">&quot;/api&quot;</span>:&#123;</span><br><span class="line">          <span class="attr">target</span>: <span class="string">&#x27;http://112.126.99.105:8010/&#x27;</span>,</span><br><span class="line">          <span class="attr">changeOrigin</span>:<span class="literal">true</span>,</span><br><span class="line">          <span class="attr">pathRewrite</span>:&#123;  <span class="comment">//重写路径，将所有api重写为空</span></span><br><span class="line">            <span class="string">&#x27;^/api&#x27;</span>:<span class="string">&#x27;&#x27;</span></span><br><span class="line">          &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="在项目中配置接口请求和数据异步处理"><a href="#在项目中配置接口请求和数据异步处理" class="headerlink" title="在项目中配置接口请求和数据异步处理"></a>在项目中配置接口请求和数据异步处理</h3><blockquote>
<p>yarn add axios<br>  yarn add vue-axios</p>
</blockquote>
<figure class="highlight js"><figcaption><span>main.js 入口文件</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">Vue</span> <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"><span class="keyword">import</span> vueAxios <span class="keyword">from</span> <span class="string">&#x27;vue-axios&#x27;</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="title function_">use</span>(vueAxios,axios)</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><figcaption><span>/require.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">Vue</span> <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"><span class="comment">//请求拦截器</span></span><br><span class="line">axios.<span class="property">interceptors</span>.<span class="property">request</span>.<span class="title function_">use</span>(<span class="keyword">function</span> (<span class="params">config</span>) &#123;</span><br><span class="line">  <span class="comment">//添加身份验证</span></span><br><span class="line">  <span class="comment">// if(config.headers.Authorization === undefined) &#123;</span></span><br><span class="line">  <span class="comment">//   config.headers.Authorization = &#x27;Bearer &#x27; + store.state.token</span></span><br><span class="line">  <span class="comment">// &#125;</span></span><br><span class="line">  <span class="keyword">return</span> config;</span><br><span class="line">&#125;, <span class="keyword">function</span> (<span class="params">error</span>) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(error)</span><br><span class="line">  <span class="keyword">return</span> <span class="title class_">Promise</span>.<span class="title function_">reject</span>(error);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">require</span>(<span class="params">option</span>)&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">fulfill,reject</span>)=&gt;</span>&#123;</span><br><span class="line">    <span class="title class_">Vue</span>.<span class="title function_">axios</span>(option).<span class="title function_">then</span>(<span class="function">(<span class="params">res</span>)=&gt;</span>&#123;</span><br><span class="line">      <span class="title function_">fulfill</span>(res)</span><br><span class="line">    &#125;).<span class="title function_">catch</span>(<span class="function">(<span class="params">rej</span>)=&gt;</span>&#123;</span><br><span class="line">      <span class="keyword">if</span>(rej.<span class="property">response</span>)&#123;</span><br><span class="line">        <span class="title function_">reject</span>(rej.<span class="property">response</span>)</span><br><span class="line">      &#125;<span class="keyword">else</span> <span class="keyword">if</span>(rej.<span class="property">require</span>)&#123;</span><br><span class="line">        <span class="title function_">reject</span>(rej.<span class="property">require</span>)</span><br><span class="line">      &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="title function_">reject</span>(rej.<span class="property">message</span>)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="built_in">require</span>;</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><figcaption><span>/config/geteway.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// export const network=&#x27;https://api.ixiaowai.cn&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> network=<span class="string">&#x27;&#x27;</span> <span class="comment">//通过vue-cli设置服务器代理，在上线前需要改正</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> auto=<span class="string">&#x27;api&#x27;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><figcaption><span>vue.config.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span>=&#123;</span><br><span class="line">  <span class="attr">devServer</span>:&#123;</span><br><span class="line">    <span class="attr">proxy</span>:&#123;</span><br><span class="line">      <span class="string">&quot;/api&quot;</span>:&#123;</span><br><span class="line">        <span class="attr">target</span>: <span class="string">&#x27;https://api.ixiaowai.cn/&#x27;</span>,</span><br><span class="line">        <span class="attr">changeOrigin</span>:<span class="literal">true</span>,</span><br><span class="line">        <span class="attr">pathRewrite</span>:&#123; <span class="comment">//重写请求路径，将api标记清除</span></span><br><span class="line">          <span class="string">&#x27;^/api&#x27;</span>:<span class="string">&#x27;&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><figcaption><span>/api/other.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requires <span class="keyword">from</span> <span class="string">&#x27;@/utils/require&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;network,auto&#125; <span class="keyword">from</span> <span class="string">&#x27;@/config/geteway&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> saying=<span class="keyword">function</span>(<span class="params">params</span>)&#123;  <span class="comment">//接口请求</span></span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">require</span>(&#123;</span><br><span class="line">    <span class="attr">url</span>:<span class="string">`<span class="subst">$&#123;network&#125;</span>/<span class="subst">$&#123;auto&#125;</span>/ylapi.php`</span>,</span><br><span class="line">    <span class="attr">method</span>:<span class="string">&#x27;GET&#x27;</span>,</span><br><span class="line">    params</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="生产代码上路由按需加载"><a href="#生产代码上路由按需加载" class="headerlink" title="生产代码上路由按需加载"></a>生产代码上路由按需加载</h3><p>基于vue-cli</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 通过路由切割</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">Home</span>=(<span class="params"></span>)=&gt;<span class="title function_">import</span>(<span class="string">&#x27;@/components/Home.vue&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> routers=[</span><br><span class="line">  &#123;<span class="attr">path</span>:<span class="string">&#x27;/&#x27;</span>,<span class="attr">component</span>:<span class="title class_">Home</span>&#125;,  <span class="comment">//在通过路由进行引用，就可以对路由进行切割</span></span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="comment">// 通过异步组件切割：使用 import按需加载的方式也可以进行切割，但如果网络不好就会空白一片，</span></span><br><span class="line"><span class="comment">// 所以需要显示加载中的话就需要用到异步组件的方式</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">AsyncComponent</span> = (<span class="params"></span>) =&gt; (&#123;</span><br><span class="line">  <span class="attr">component</span>: <span class="title function_">import</span>(<span class="string">&#x27;@/MyComponent.vue&#x27;</span>), <span class="comment">// 需要加载的组件 (应该是一个 `Promise` 对象)</span></span><br><span class="line">  <span class="attr">loading</span>: <span class="title class_">LoadingComponent</span>, <span class="comment">// 异步组件加载时使用的组件</span></span><br><span class="line">  <span class="attr">error</span>: <span class="title class_">ErrorComponent</span>, <span class="comment">// 加载失败时使用的组件</span></span><br><span class="line">  <span class="attr">delay</span>: <span class="number">200</span>, <span class="comment">// 展示加载时组件的延时时间。默认值是 200 (毫秒)</span></span><br><span class="line">  <span class="attr">timeout</span>: <span class="number">3000</span>   <span class="comment">// 超时时间和组件加载都超时， 则使用加载失败时使用的组件。默认值是：`Infinity`</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><h3 id="Uncaught-TypeError-Cannot-read-properties-of-undefined-reading-‘install‘-报错，页面全空白"><a href="#Uncaught-TypeError-Cannot-read-properties-of-undefined-reading-‘install‘-报错，页面全空白" class="headerlink" title="Uncaught TypeError: Cannot read properties of undefined (reading ‘install‘)报错，页面全空白"></a>Uncaught TypeError: Cannot read properties of undefined (reading ‘install‘)报错，页面全空白</h3><p><code>vue-router</code>的版本问题，vue-router4区别于3 ，没有了默认导出export default的模块，需要使用 import {createrRouter} from ‘vue-router’的方式进行导入；之后在页面引用<code>&lt;router-view&gt;</code>的时候同样需要进行模块引入；vue.use()；建议将vue-router 替换成 3.1.3版本；</p>
<h3 id="在Vue-中-import-引入的路径中的-指代什么？"><a href="#在Vue-中-import-引入的路径中的-指代什么？" class="headerlink" title="在Vue 中 import 引入的路径中的 @ 指代什么？"></a>在Vue 中 import 引入的路径中的 @ 指代什么？</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">import require from &#x27;@/utils/require&#x27;</span><br></pre></td></tr></table></figure>

<p>在Vue中，@ 是指代 /src 目录，是为了防止复杂路径导致的出错;</p>
<h3 id="请求报错-No-‘Access-Control-Allow-Origin’？"><a href="#请求报错-No-‘Access-Control-Allow-Origin’？" class="headerlink" title="请求报错 No ‘Access-Control-Allow-Origin’？"></a>请求报错 No ‘Access-Control-Allow-Origin’？</h3><p>该错误很大可能是属于请求跨域报错，需要使用vue-cli设置代理，在根目录创建 <code>vue.config.js</code></p>
<p>```js vue.config.js<br>module.exports={<br>  devServer:{<br>    proxy:{<br>      ‘user/‘:{<br>        target:”https:xxxx”,<br>        changeOrigin:true,<br>      }<br>    }<br>  }<br>}</p>
  </article>
  <aside class="table-content" id="site-toc">
  <div class="table-content-title">
    <i class="fa fa-arrow-right fa-lg" id="site-toc-hide-btn"></i>
    <span>目录</span>
  </div>
  <div class="table-content-main">
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E9%A1%BB%E7%9F%A5"><span class="toc-text">使用须知</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%80%E5%90%AF%E9%A1%B9%E7%9B%AE"><span class="toc-text">开启项目</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81"><span class="toc-text">示例代码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E6%89%A9%E5%B1%95"><span class="toc-text">相关扩展</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8%E9%A1%B9%E7%9B%AE%E4%B8%AD%E9%85%8D%E7%BD%AE%E6%A8%A1%E5%9D%97%E7%9A%84%E7%9B%B8%E5%85%B3%E7%9B%AE%E5%BD%95"><span class="toc-text">在项目中配置模块的相关目录</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8%E6%89%8B%E6%9C%BA%E7%AB%AF%E8%AE%BE%E7%BD%AE%E5%8C%BA%E5%88%86%E5%89%8D%E8%BF%9B%E5%90%8E%E9%80%80%E7%9A%84%E4%BB%A3%E7%A0%81"><span class="toc-text">在手机端设置区分前进后退的代码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8%E6%9C%AC%E5%9C%B0%E9%A1%B9%E7%9B%AE%E4%B8%AD%E9%85%8D%E7%BD%AEproxy%E8%A7%A3%E5%86%B3%E8%B7%A8%E5%9F%9F%E8%AF%B7%E6%B1%82"><span class="toc-text">在本地项目中配置proxy解决跨域请求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8%E9%A1%B9%E7%9B%AE%E4%B8%AD%E9%85%8D%E7%BD%AE%E6%8E%A5%E5%8F%A3%E8%AF%B7%E6%B1%82%E5%92%8C%E6%95%B0%E6%8D%AE%E5%BC%82%E6%AD%A5%E5%A4%84%E7%90%86"><span class="toc-text">在项目中配置接口请求和数据异步处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%94%9F%E4%BA%A7%E4%BB%A3%E7%A0%81%E4%B8%8A%E8%B7%AF%E7%94%B1%E6%8C%89%E9%9C%80%E5%8A%A0%E8%BD%BD"><span class="toc-text">生产代码上路由按需加载</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%97%AE%E9%A2%98"><span class="toc-text">问题</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Uncaught-TypeError-Cannot-read-properties-of-undefined-reading-%E2%80%98install%E2%80%98-%E6%8A%A5%E9%94%99%EF%BC%8C%E9%A1%B5%E9%9D%A2%E5%85%A8%E7%A9%BA%E7%99%BD"><span class="toc-text">Uncaught TypeError: Cannot read properties of undefined (reading ‘install‘)报错，页面全空白</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8Vue-%E4%B8%AD-import-%E5%BC%95%E5%85%A5%E7%9A%84%E8%B7%AF%E5%BE%84%E4%B8%AD%E7%9A%84-%E6%8C%87%E4%BB%A3%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="toc-text">在Vue 中 import 引入的路径中的 @ 指代什么？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AF%B7%E6%B1%82%E6%8A%A5%E9%94%99-No-%E2%80%98Access-Control-Allow-Origin%E2%80%99%EF%BC%9F"><span class="toc-text">请求报错 No ‘Access-Control-Allow-Origin’？</span></a></li></ol></li></ol>
  </div>
</aside>

  
  
  
</div>

    </main>
    
    
<div class="site-footer-wrapper">
<!--
  <div class="footer-top">
    <a class="left" href="/blog/2020/12/10/Yarn%E5%92%8Cnpm%E7%9A%84%E5%91%BD%E4%BB%A4%E9%9B%86%E5%92%8C%E9%97%AE%E9%A2%98/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a class="right" href="/blog/2020/12/09/%E5%8F%91%E5%B8%83%E5%92%8C%E7%BB%B4%E6%8A%A4%E5%B1%9E%E4%BA%8E%E8%87%AA%E5%B7%B1%E7%9A%84NPM%E5%8C%85/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
  </div>
-->
  <footer class="site-footer">
    
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">网站推荐</h5>
          
            <span class="site-footer-item">
              <a href="https://tympanus.net/codrops/" target="_blank">Codrops</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://taobaofed.org/" target="_blank">淘宝前端团队(FED)</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">文档教程</h5>
          
            <span class="site-footer-item">
              <a href="https://es6.ruanyifeng.com/" target="_blank">ES6入门</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://developer.mozilla.org/zh-CN/docs/Web" target="_blank">MDN Web文档</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">兴趣前沿</h5>
          
            <span class="site-footer-item">
              <a href="https://threejs.org/" target="_blank">threejs</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://github.com/justadudewhohacks/face-api.js" target="_blank">face-api</a>
            </span>
          
        </div>
      
    
    <div class="site-footer-info">
      <i class="fa fa-clock-o"></i> 本站已稳定运行<span id="site-time"></span>
    </div>
    
      <div class="site-footer-info">
        <i class="fa fa-paw"></i> 您是本站第 <span id="site-count"></span> 位访客
      </div>
    
    
      <div class="site-footer-info">
        <i class="fa fa-at"></i> Email: xianbin.me@qq.com
      </div>
    
<!--     <div class="site-footer-info">
      <i class="fa fa-copyright"></i> 
      2019 <a href="https://github.com/dongyuanxin/theme-ad/" target="_blank">Theme-AD</a>.
      Created by <a href="https://godbmw.com/" target="_blank">GodBMW</a>.
      All rights reserved.
    </div> -->
  </footer>
</div>
    <div id="site-layer" style="display:none;">
  <div class="site-layer-content">
    <div class="site-layer-header">
      <span class="site-layer-header-title" id="site-layer-title"></span>
      <i class="fa fa-close" id="site-layer-close"></i>
    </div>
    <div class="site-layer-body" id="site-layer-container">
      <div class="site-layer-input" id="site-layer-search" style="display: none;">
        <input type="text">
        <i class="fa fa-search"></i>
      </div>
      
        <div class="site-layer-reward" id="site-layer-reward" style="display: none;">
          
            <div>
              <img src="/blog/images/wechat.png" alt="WeChat">
              
                <p>WeChat</p>
              
            </div>
          
            <div>
              <img src="/blog/images/alipay.png" alt="AliPay">
              
                <p>AliPay</p>
              
            </div>
          
        </div>
      
      <div id="site-layer-welcome" style="display:none;"></div>
    </div>
  </div>
</div>
    

<div class="bottom-bar">

  <div class="bottom-bar-left">
<!--
    <a href="/blog/2020/12/10/Yarn%E5%92%8Cnpm%E7%9A%84%E5%91%BD%E4%BB%A4%E9%9B%86%E5%92%8C%E9%97%AE%E9%A2%98/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a href="/blog/2020/12/09/%E5%8F%91%E5%B8%83%E5%92%8C%E7%BB%B4%E6%8A%A4%E5%B1%9E%E4%BA%8E%E8%87%AA%E5%B7%B1%E7%9A%84NPM%E5%8C%85/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
-->
  </div>

  
  
  
  <div class="bottom-bar-right">
    <a href="javascript:void(0);" data-enable="true" id="site-toc-show-btn">
      <i class="fa fa-bars"></i>
    </a>
    
      <a href="#site-comment" data-enable="true">
        <i class="fa fa-commenting"></i>
      </a>
    
    <a href="javascript:void(0);" id="site-toggle-share-btn">
      <i class="fa fa-share-alt"></i>
    </a>
    
    <a href="javascript:void(0);" id="back-top-btn">
      <i class="fa fa-chevron-up"></i>
    </a>
  </div>
</div>
    <div id="share-btn">
  
  
  
    <a id="share-btn-weibo" href="javascript:void(0);" target="_blank">
      <i class="fa fa-weibo"></i>
    </a>
  
  
    <a id="share-btn-qq" href="javascript:void(0);" target="_blank">
      <i class="fa fa-qq"></i>
    </a>
  
  
</div>
    





    
  </body>
</html>